﻿using System.ComponentModel;
using Loki;
using Loki.Commands;
using Luna.Model.Security;

namespace Luna.UI.Install
{
    public class FirstUserViewModel : LunaViewModel
    {
        #region Properties
        #region  Name
        private static PropertyChangedEventArgs _NameChangeArgs = ObservableHelper.CreateArgs<FirstUserViewModel>(x => x.Name);

        private string _Name;

        public string Name
        {
            get
            {
                return _Name;
            }

            set
            {
                if (value != _Name)
                {
                    _Name = value;
                    NotifyChanged(_NameChangeArgs);
                }
            }
        }
        #endregion

        #region  SurName
        private static PropertyChangedEventArgs _SurNameChangeArgs = ObservableHelper.CreateArgs<FirstUserViewModel>(x => x.SurName);

        private string _SurName;

        public string SurName
        {
            get
            {
                return _SurName;
            }

            set
            {
                if (value != _SurName)
                {
                    _SurName = value;
                    NotifyChanged(_SurNameChangeArgs);
                }
            }
        }
        #endregion

        #region  Login
        private static PropertyChangedEventArgs _LoginChangeArgs = ObservableHelper.CreateArgs<FirstUserViewModel>(x => x.Login);

        private string _Login;

        public string Login
        {
            get
            {
                return _Login;
            }

            set
            {
                if (value != _Login)
                {
                    _Login = value;
                    NotifyChanged(_LoginChangeArgs);
                }
            }
        }
        #endregion

        #region  Password
        private static PropertyChangedEventArgs _PasswordChangeArgs = ObservableHelper.CreateArgs<FirstUserViewModel>(x => x.Password);

        private string _Password;

        public string Password
        {
            get
            {
                return _Password;
            }

            set
            {
                if (value != _Password)
                {
                    _Password = value;
                    NotifyChanged(_PasswordChangeArgs);
                }
            }
        }
        #endregion

        #region  Confirm
        private static PropertyChangedEventArgs _ConfirmChangeArgs = ObservableHelper.CreateArgs<FirstUserViewModel>(x => x.Confirm);

        private string _Confirm;

        public string Confirm
        {
            get
            {
                return _Confirm;
            }

            set
            {
                if (value != _Confirm)
                {
                    _Confirm = value;
                    NotifyChanged(_ConfirmChangeArgs);
                }
            }
        }
        #endregion
        #endregion

        public FirstUserViewModel()
        {
            CreateUser = Commands.Create();
            Cancel = Commands.Create();

            Commands.Handle(CreateUser, CreateUser_CanExecute, CreateUser_Execute);
            Commands.Handle(Cancel, Cancel_Execute);
        }

        private void CreateUser_CanExecute(object P_Sender, CanExecuteCommandEventArgs e)
        {
            e.CanExecute =
                   !string.IsNullOrEmpty(Name)
                && !string.IsNullOrEmpty(SurName)
                && !string.IsNullOrEmpty(Login)
                && !string.IsNullOrEmpty(Password) && (Password == Confirm);
        }

        private void CreateUser_Execute(object P_Sender, CommandEventArgs e)
        {
            User L_User = new User() { Name = Name, Login = Login, Surname = SurName };
            L_User.SetPassword(Password);

            ApplicationModel.Security.SaveUser(L_User);
            ApplicationModel.Security.LogonUser(L_User.Login, Password);

            ApplicationCommands.START.Execute(null);
            Close.Execute(null);
        }

        private void Cancel_Execute(object P_Sender, CommandEventArgs e)
        {
            ApplicationCommands.EXIT.Execute(null);
        }

        #region Commands
        /// <summary>
        /// Gets the create user command.
        /// </summary>
        public ICommand CreateUser
        {
            get;
            private set;
        }

        public ICommand Cancel
        {
            get;
            private set;
        }
        #endregion
    }
}
